<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隔行换色</title>
    <script>
        window.onload = function () {
            var elementById = document.getElementById('tbl');
            let length = elementById.tBodies[0].rows.length;
            for (var i = 0; i < length; i++) {
                if (i % 2 == 0) {
                    elementById.tBodies[0].rows[i].style.backgroundColor = "pink";
                } else {
                    elementById.tBodies[0].rows[i].style.backgroundColor = "gold";
                }
            }
        }
    </script>
</head>
<body>
<table border="1" width="500" height="50" align="center" id="tbl">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>专业</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>20180201</td>
        <td>刘备</td>
        <td>全栈</td>
    </tr>
    <tr>
        <td>20180202</td>
        <td>曹操</td>
        <td>java</td>
    </tr>
    <tr>
        <td>20180203</td>
        <td>孙权</td>
        <td>Python</td>
    </tr>
    <tr>
        <td>20180204</td>
        <td>刘表</td>
        <td>大数据</td>
    </tr>
    </tbody>
</table>
</body>
</html>